package com.itheima.service;

import com.itheima.domain.Student;
import com.itheima.mapper.StudentMapper;
import com.itheima.utils.MyBatisUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class Test {


    @org.junit.Test
    public void selectById(){

        //1.获取SqlSession对象
        SqlSession sqlSession = MyBatisUtils.getSqlSession();

        //2.动态获取StudentMapper的实现类对象
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

        //3.mapper调用方法
        Student student = mapper.selectById(1);
        System.out.println(student);

        //释放资源
        sqlSession.close();

    }

    @org.junit.Test
    public void save(){

        //1.获取SqlSession对象
        SqlSession sqlSession = MyBatisUtils.getSqlSession();

        //2.动态获取StudentMapper的实现类对象
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

        //3.mapper调用方法
        Student student = new Student(18, "嗯嗯", 34, "男");
        mapper.save(student);
        sqlSession.commit();
        //释放资源
        sqlSession.close();

    }

    @org.junit.Test
    public void selectByNameOrAge(){

        //1.获取SqlSession对象
        SqlSession sqlSession = MyBatisUtils.getSqlSession();

        //2.动态获取StudentMapper的实现类对象
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

        //3.mapper调用方法
        List<Student> list  = mapper.selectByIdOrNameOrAgeOrGneder(1,"", 0,"");
        for (Student stu : list) {
            System.out.println(stu);
        }
        //释放资源
        sqlSession.close();

    }

}
